﻿package control
{
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.net.*;
    
    import control.IProcessInformer;

    /**
     * Dispatched when image loaded
     */
    [Event(name="complete", type="flash.events.Event")]
    
    /**
     * Simple loaded images.
     */
    public class Picture extends Sprite
    {
        /**
	 * Any preloader implemented IProcessInformer interface.
	 */
        public var informer: IProcessInformer = null;
	
        /**
	 * URI to picture
	 */
        public var uri: String = null;

	/**
	 *  @constructor
	 */
	public function Picture(uri:String=null)
	{
	    this.uri = uri;
	}
        
        
        /**
	 * Load image
	 */
        public function load():void
        {
            if (uri == null || uri == "")
            {
                trace("WAR: Picture.load() Can't load from null url");
                return;
            }
	    
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImageCompleteHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadImageProcessHandler);
	    
            try 
            {
		var request:URLRequest = new URLRequest(uri);
		var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain, SecurityDomain.currentDomain);
            	loader.load(request, flash.system.Capabilities.isDebugger ? null : context);
            }
            catch (error:Error)
            {
            	trace("ERR: " + error.message);
            }
        }
        
        /**
	 * Unifide fit to fixed width and height
	 */
        public function fitToFixedSize(w:Number, h:Number):void
        {
            var minSide: Number = w > h ? h : w;
            scaleX = minSide / loader.width;
            scaleY = minSide / loader.width;
        }
	
	/**
	 * Clone bitmap data of presented image
	 */
	public function clone():BitmapData
	{
	    var bmd: BitmapData = new BitmapData(loader.width, loader.height);
	    bmd.draw(loader);
	    return bmd;
	}




	//---------------------------------------------------------------------
	//
	// Private logic
	//
	//---------------------------------------------------------------------
	
        /**  **/
        private var loader:Loader;
        
        /**
	 * Complete loading image
	 */
        private function loadImageCompleteHandler(event:Event):void
        {
            if (informer && contains(informer as DisplayObject))
                removeChild(DisplayObject(informer));
            addChild(loader);
            dispatchEvent(event);
        }
        
        /**
	 * Loadding image progress
	 */
        private function loadImageProcessHandler(event:ProgressEvent):void
        {
            if (informer) 
            {
                informer.percent = event.bytesLoaded / event.bytesTotal;
            }
            else
            {
                informer = new ProgressBar() as IProcessInformer;
                addChild(DisplayObject(informer));
                (informer as DisplayObject).x = 10;
                (informer as DisplayObject).y = 10;
            }
        }
        
        /**
	 *
	 */
        private function errorHandler(event:IOErrorEvent):void
        {
            trace("ERR: Picture: " + event.text);
        }
        
    }
}